package com.briup.product_source.web.controller;

import com.briup.product_source.bean.BaseAccount;
import com.briup.product_source.service.AccountService;
import com.briup.product_source.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;


/**
 * 登录相关接口
 * @Author lining
 * @Date 2023/6/19
 */
@Api(tags = "登录相关模块")
@RestController
public class LoginController {
    //1.controller被ioc创建 整个程序中只有一个对象

    //2.tomcat是多线程程序；对象的属性可以被所有线程访问（用户）

    //3.不会在controller 定义与用户相关的变量 token username
    //private static String token;


    @Autowired
    private AccountService service;

    @ApiOperation("获取当前登录用户信息接口")
    @GetMapping("/me")
    //@ApiIgnore 在swagger界面中忽略指定的参数
    public Response getCurrentAccount(@ApiIgnore @RequestHeader String token){
        //调用service层代码
        System.out.println(token);
        BaseAccount account = service.findLoginUser(token);
        //将service层返回结果，返回给前端 json
        return Response.success(account);
    }


    @ApiOperation("获取当前登录用户信息接口")
    @GetMapping("/me2")
    //@ApiIgnore 在swagger界面中忽略指定的参数
    public Response getCurrentAccount2(){
        //获取请求对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String token = request.getHeader("token");

        //调用service层代码
        System.out.println(token);
        //service.findLoginUser(token);
        return Response.success();
    }
    @ApiOperation("获取当前登录用户信息接口")
    @GetMapping("/me3")
    //request 表示浏览器发送的请求报文。不表示具体参数，swagger不会将request作为参数
    public Response getCurrentAccount2(HttpServletRequest request){

        String token = request.getHeader("token");

        //调用service层代码
        System.out.println(token);
        //service.findLoginUser(token);
        return Response.success();
    }
}
